#include "CameraThread.h"
#include <QEventLoop>

CameraThread::CameraThread(int cameraId,
                           QMutex *mutex,
                           QWaitCondition *trigger,
                           QObject *parent)
    : QThread(parent)
{
    m_worker = new CameraWorker(cameraId, mutex, trigger);
    m_worker->moveToThread(this);
    connect(this, &QThread::started, m_worker, &CameraWorker::capture);
    connect(m_worker, &CameraWorker::finished, this, &QThread::quit);
    connect(m_worker, &CameraWorker::frameReady,
            this, &CameraThread::frameReady, Qt::QueuedConnection);
}
CameraThread::~CameraThread() { requestStop(); }
void CameraThread::requestStop()
{
    m_worker->stop();
    quit();
    wait();
}
void CameraThread::run()
{
    QEventLoop loop;
    QThread::run();
}
